package com.cheneyjoo.view;

import com.cheneyjoo.base.Login;
import com.cheneyjoo.util.CheneyScanner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Index {
    private Socket socket = null;
    private PrintStream out = null;
    private BufferedReader in = null;
    private Login login;

    public Index() {
    }

    public Index(Socket socket) throws IOException {
        this.socket = socket;
        this.out = new PrintStream(this.socket.getOutputStream());
        this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
    }

    /**
     * +
     * 首页
     *
     * @throws IOException
     */
    public void index() throws IOException {
        System.out.println("------------------------------------------");
        System.out.print(" [1]用户登录");
        System.out.println("\t [2]管理员登录");
        System.out.println(" [0]退出");
        System.out.println("请选择对应的功能编码：");
        System.out.println("------------------------------------------");
        String menu = CheneyScanner.reader.readLine();
        switch (menu) {
            case "0":
                out.println("bye");
                String readLine = this.in.readLine();
                if ("*server close*".equals(readLine)) {
                    this.in.close();
                    this.out.close();
                }
                return;
            case "1":
                login = new UserLogin(socket, in, out);
                break;
            case "2":
                login = new ManagerLogin(socket, in, out);
                break;
        }
        login.login();
    }
}
